package com.example.li_han.servlet;

import com.example.li_han.service.NewsService;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@WebServlet(name = "DeleteBatchNews", value = "/DeleteBatchNews")

public class DeleteBatchNews extends HttpServlet {
    //批量删除
    private String message;
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");

        // Hello
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + message + "</h1>");
        out.println("</body></html>");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader br = new BufferedReader(req.getReader());
        StringBuilder sbuffer = new StringBuilder();
        String s;

        // 按行读取请求体
        while((s = br.readLine()) != null) {
            sbuffer.append(s);
        }

        String requestBody = sbuffer.toString();
        System.out.println("收到请求体: " + requestBody);

        Gson gson = new Gson();
        Map<String, Object> resultMap = new HashMap<>();

        // 检查请求体是否为空
        if (requestBody == null || requestBody.trim().isEmpty()) {
            resultMap.put("code", 500);
            resultMap.put("msg", "请求体为空");
            resp.getWriter().append(gson.toJson(resultMap));
            return;
        }

        try {
            // 解析JSON
            Map<String, Object> newsIds = gson.fromJson(requestBody, Map.class);

            // 检查解析结果是否为null
            if (newsIds == null) {
                resultMap.put("code", 500);
                resultMap.put("msg", "JSON解析失败");
                resp.getWriter().append(gson.toJson(resultMap));
                return;
            }

            // 检查ids字段是否存在
            if (!newsIds.containsKey("ids")) {
                resultMap.put("code", 500);
                resultMap.put("msg", "缺少ids字段");
                resp.getWriter().append(gson.toJson(resultMap));
                return;
            }

            ArrayList<Double> list = (ArrayList<Double>) newsIds.get("ids");

            // 检查ids列表是否为空
            if (list == null || list.isEmpty()) {
                resultMap.put("code", 500);
                resultMap.put("msg", "ids列表为空");
                resp.getWriter().append(gson.toJson(resultMap));
                return;
            }

            List<Integer> integerList = list.stream()
                    .map(Double::intValue)
                    .collect(Collectors.toList());

            System.out.println("要删除的ID列表: " + integerList);

            // 调用业务方法
            NewsService newsService = new NewsService();
            int flag = newsService.deleteBatchNews(integerList);

            if(flag > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "删除成功");
                resultMap.put("deletedCount", flag);
            } else {
                resultMap.put("code", 500);
                resultMap.put("msg", "删除失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", 500);
            resultMap.put("msg", "服务器异常: " + e.getMessage());
        }

        // 返回响应
        String json = gson.toJson(resultMap);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().append(json);
    }
}